package com.app.aipass.fragment.my.task;

import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.databinding.ObservableField;

import com.app.aipass.R;
import com.app.aipass.entity.FinanceOrderAllEntity;

import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;

/**
 * @author: woking
 * @date: 2021/6/9
 * @description：
 */
public class TaskTabItemViewModel extends ItemViewModel<TaskTabViewModel> {
    public ObservableField<FinanceOrderAllEntity> entity = new ObservableField<>();
    public Drawable drawableImg;

    public TaskTabItemViewModel(@NonNull TaskTabViewModel viewModel, FinanceOrderAllEntity entity) {
        super(viewModel);
        this.entity.set(entity);
        //ImageView的占位图片，可以解决RecyclerView中图片错误问题
        drawableImg = ContextCompat.getDrawable(viewModel.getApplication(), R.mipmap.ic_launcher);
    }

    public boolean isRunIng()
    {
        if(viewModel.type.getValue() == 1)
        {
            return true;
        }
        return false;
    }

    public String setQuantity()
    {
        if(viewModel.type.getValue() == 1)//任务商城
        {
//            Log.e("TAG",entity.get().progress+"=======setQuantity===========HTTP========="+entity.get().max_num);
            String str = entity.get().progress + "/" +entity.get().max_num;
            return str;
//            return "str";
        }
        return entity.get().quantity + "";
    }
    public String setHuoYueDu()
    {
        return entity.get().active_value + "";
    }
    public String setPrice()
    {
        return entity.get().price + "AD/个";
    }
    public String setZhouQi()
    {
        return entity.get().lease_days + "天";
    }
    public String setShouYiLv()
    {
        return entity.get().profit_ratio + "%";
    }
    public String setTotalAD()
    {
        return entity.get().all_release_amount + "AD";
    }
    public String setDayAD()
    {
        return entity.get().days_release_amount + "AD";
    }
    public String setQuantityType()
    {
        if(viewModel.type.getValue() == 1)
        {
            return "限量";
        }
        return "数量";
    }
    public String setTitleType()
    {
        if(viewModel.type.getValue() == 1)
        {
            return entity.get().title;
        }
        return entity.get().product.data.title;
    }

    public String setImageUrl()
    {
        String url = "";
        if(viewModel.type.getValue() == 1)
        {
            url = entity.get().pic;
        }
        else
        {
            url = entity.get().product.data.pic;
        }

        if(url.startsWith("http"))
        {
            return url;
        }
        return "https://qiniu.xiaopankeji.com/"+ url;
    }
    /**
     * 获取position的方式有很多种,indexOf是其中一种，常见的还有在Adapter中、ItemBinding.of回调里
     *
     * @return
     */
    public int getPosition() {
        return viewModel.getItemPosition(this);
    }

    //条目的点击事件
    public BindingCommand itemClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
//                Bundle mBundle = new Bundle();
//                mBundle.putParcelable("entity", entity.get());
//                viewModel.startContainerActivity(DetailFragment.class.getCanonicalName(), mBundle);

//            viewModel.startContainerActivity(TaskDetailFragment.class.getCanonicalName());
        }
    });
    //条目的长按事件
    public BindingCommand itemLongClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //以前是使用Messenger发送事件，在NetWorkViewModel中完成删除逻辑
//            Messenger.getDefault().send(NetWorkItemViewModel.this, NetWorkViewModel.TOKEN_NETWORKVIEWMODEL_DELTE_ITEM);
            //现在ItemViewModel中存在ViewModel引用，可以直接拿到LiveData去做删除
//            ToastUtils.showShort(entity.get().getName());
        }
    });
//    /**
//     * 可以在xml中使用binding:currentView="@{viewModel.titleTextView}" 拿到这个控件的引用, 但是强烈不推荐这样做，避免内存泄漏
//     **/
//    private TextView tv;
//    //将标题TextView控件回调到ViewModel中
//    public BindingCommand<TextView> titleTextView = new BindingCommand(new BindingConsumer<TextView>() {
//        @Override
//        public void call(TextView tv) {
//            NetWorkItemViewModel.this.tv = tv;
//        }
//    });


    public BindingCommand onClickDuiHuan = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            viewModel.duihuanItemLiveData.setValue(TaskTabItemViewModel.this);
        }
    });
}
